# -*- coding:utf-8 -*-
import json
from time import sleep

import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from ppyy.Redis import RedisClient


class PaperYY(object):
    r = RedisClient()

    def submit(self, title, author, content):
        chrome_options = Options()
        chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('--headless')
        driver = webdriver.Chrome(options=chrome_options)
        driver.set_window_size(1920, 1080)
        try:
            driver.get("https://www.paperyy.com/member_new/thesis/post.aspx")
            driver.delete_all_cookies()
            cookies = self.r.GetCookies()
            listCookie = json.loads(cookies)
            for cookie in listCookie:
                driver.add_cookie(cookie)
            driver.refresh()
            driver.find_element_by_xpath('//*[@id="edition-1"]').click()
            driver.find_element_by_xpath('//*[@id="chachong_form"]/div/div[1]/span[1]').click()
            js_content = json.dumps(content).strip('"').replace("'", '')
            js = "var element=document.getElementById('txtContent'); element.value='" + js_content + "';"
            driver.execute_script(js)
            driver.find_element_by_id('txtTitle').send_keys(title)
            driver.find_element_by_id('txtAuthor').send_keys(author)
            driver.find_element_by_id('txtContent').send_keys(' ')
            driver.find_element_by_id('btnSubmit2').click()
            driver.implicitly_wait(10)
            driver.find_element_by_id('priovid6').click()
            driver.implicitly_wait(2)
            driver.find_element_by_xpath('/html/body/div[11]/div/div[2]/div[3]/button[1]').click()
            driver.find_element_by_xpath('//*[@id="ajaxList"]/div[2]/div[1]/div/div[3]/div[1]/div').click()
            driver.find_element_by_xpath('//*[@id="ajaxList"]/div[2]/div[1]/div/div[4]/div[1]/div').click()
            driver.find_element_by_xpath('//*[@id="submitPay"]').click()
            driver.implicitly_wait(10)
        except Exception as e:
            print(e)
            pass

    def check(self):
        cookies = {}
        res = []
        listCookie = json.loads(self.r.GetCookies())
        for cookie in listCookie:
            cookies[cookie['name']] = cookie['value']
        r = requests.get(
            'https://www.paperyy.com/api/v2/thesis/thesis_list?page=1&size=20&sign=&clienttime=0.07393693966467096',
            cookies=cookies)
        data = json.loads(r.text)
        for item in data['data']['content']:
            if item['stateDesc'] == '已完成':
                report_info = item['title'] + '$$' + item['zipUrl']
                res.append(report_info)
        r = requests.post('http://39.105.230.139:5000/v2/article/autodeal', json={'reports': res})
        print(r.content)

    def login(self):
        chrome_options = Options()
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('--headless')
        driver = webdriver.Chrome(options=chrome_options)
        driver.set_window_size(1920, 1080)
        try:
            driver.get('https://www.paperyy.com/passport/login')
            driver.implicitly_wait(10)
            driver.find_element_by_xpath('/html/body/div[3]/div[1]/ul/li[2]').click()
            driver.implicitly_wait(1)
            driver.find_element_by_id('txtEmail').send_keys('17601253218')
            driver.find_element_by_id('txtUserPass1').send_keys('63505711!')
            driver.find_element_by_id('btnSubmit').click()
            dictCookies = driver.get_cookies()
            jsonCookies = json.dumps(dictCookies)
            self.r.SetCookies(jsonCookies)
            driver.quit()
        except Exception as e:
            print(e)
            driver.quit()


if __name__ == '__main__':
    bot = PaperYY()
    bot.submit('')
